<template>
	<div class="modal fade" tabindex="-1" role="dialog">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title">{{operationName}}小节</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal">
						<div class="form-group">
							<label class="col-sm-2 control-label">小节标题</label>
							<div class="col-sm-10">
								<input class="form-control" v-model="section.title">
							</div>
						</div>
						
						<div class="form-group">
							<label class="col-sm-2 control-label">课程名称</label>
							<div class="col-sm-10">
								<p class="form-control-static">{{$parent.course.name}}</p>
							</div>
						</div>
						
						<div class="form-group">
							<label class="col-sm-2 control-label">大章名称</label>
							<div class="col-sm-10">
								<p class="form-control-static">{{$parent.chapter.name}}</p>
							</div>
						</div>						
						
						<div class="form-group">
							<label class="col-sm-2 control-label">视频</label>
							<div class="col-sm-10">
								<!-- <input class="form-control" v-model="section.video"> -->
								<bigFile ref="file" text="上传视频" :afterUpload="afterUpload" :suffixs="suffixs"
									:uploadUrl="uploadUrl" :purpose="$global.constant.FILE_PURPOSE.section" />
								<div v-if="section.video" class="row">
									<video id="video" :src="section.video" controls="controls" class="col-sm-9"/>
								</div>
							</div>
						</div>	
												
<!-- 						<div class="form-group">
							<label class="col-sm-2 control-label">时长/秒</label>
							<div class="col-sm-10">
								<input class="form-control" v-model="section.time">
							</div>
						</div>	 -->
																								
						<div class="form-group">
							<label class="col-sm-2 control-label">是否收费</label>
							<div class="col-sm-10">
								<select v-model="section.charge" class="form-control">
									<option v-for="(item, index) in $parent.CHARGE" :value="item.key" :key="index">
										{{item.value}}
									</option>
								</select>
							</div>
						</div>																									
												
						<div class="form-group">
							<label class="col-sm-2 control-label">顺序</label>
							<div class="col-sm-10">
								<input class="form-control" v-model="section.sort">
							</div>
						</div>							
						
						
						
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-primary" @click="save">{{operationName}}</button>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal-dialog -->
	</div><!-- /.modal -->
</template>

<script>
import bigFile from '@/views/components/bigFile'
export default{
	data() {
		return {
			suffixs: ['mp4'],
			uploadUrl: process.env.VUE_APP_SERVER + 'file/admin/oss/upload/video/section',
		}
	},
	props: {
		section: Object
	},
	computed: {
		operationName() {
			return this.section.id ? "修改" : "新增"
		}
	},
	components:{
		bigFile
	},
	methods: {
		save() {
			this.section.courseId = this.$parent.course.id
			this.section.chapterId = this.$parent.chapter.id
			this.getTime()
			let url = process.env.VUE_APP_SERVER + 'business/admin/section/save'
			this.$axios.post(url, this.section)
				.then(res => {
					if(res.data.status === 1) {
						this.$toast(this.operationName + '小节成功', this.$global.toast.success)
						$('#save-modal').modal('hide')
						this.$parent.list(1)
					}else {
						this.$toast(res.data.msg, this.$global.toast.fail)
					}
				})
		},
		// 文件上传完成后回调
		afterUpload(resp) {
			this.section.video = resp.data.path
			this.$forceUpdate()
			// this.getTime()
		},
		getTime() {
			let a = document.getElementById('video')
			this.section.time = a.duration
		}
	}
}	
</script>

<style>
</style>
